/*
 *说明：用户可选择的授权资源 脚本
 *
 *@author liuxun 2012-03-26
*/

var opeForm;
var grid;

$(function(){
	opeForm = $('#opeForm');
	$('#replyBtn').click(replyOperation);
	$('#refuseBtn').click(refuseOperation);
});

//翻页时自动调用此函数
function onLoadPrev(msg){
	var queryParams=grid.datagrid('options').queryParams;
	queryParams['firstQuery']=false;
}

//是否长期有效  is_longterm_valid
function is_longterm_validChange(rowindex){
	var row_is_longterm_valid = $("#is_longterm_valid_"+rowindex).val();
	if(row_is_longterm_valid == '1'){
		$('#start_time_'+ rowindex).val('');
		$('#start_time_'+ rowindex).attr('readonly', true);
		//$('#start_time_'+ rowindex).attr('disabled', true);
		$('#available_length_'+ rowindex).attr('readonly', true);
		//$('#available_length_'+ rowindex).attr('disabled', true);
		$('#available_length_'+ rowindex).val('');
		$('#start_time_'+ rowindex).attr('required', true);
		$('#available_length_'+ rowindex).attr('required', true);
		document.getElementById("img_"+rowindex).style.display = 'none';
	}else{
		$('#start_time_'+ rowindex).val('');
		$('#start_time_'+ rowindex).attr('readonly', true);
		//$('#start_time_'+ rowindex).attr('disabled', false);
		$('#available_length_'+ rowindex).attr('readonly', false);
		//$('#available_length_'+ rowindex).attr('disabled', false);
		$('#start_time_'+ rowindex).attr('required', false);
		$('#available_length_'+ rowindex).attr('required', false);
		document.getElementById("img_"+rowindex).style.display = 'inline';
	}
}

//选择命令策略
function select_cmdpolicy(rowindex){
	var action = 'D_ACL!showCmdPolicySelect.do?model.acl_id=';
	var row = showModalWin(action,'A2');
	
	if (row) {
		$('#policy_id_' + rowindex).val(row.cmd_policy_id);
		$('#policy_name_' + rowindex).val(row.cmd_policy_name);
	}
}

//同意按钮操作
function replyOperation() {
	var is_check = true;
	var message = '';
	$('#listTable tbody tr').each(function(i,n){
		var is_longterm_valid = $(this).find('td').eq(7).find('#is_longterm_valid_'+i).val();
		var start_time = $('#start_time_'+i).val();
		var available_length = $('#available_length_' + i).val();
		
		if(is_longterm_valid == '0'){
			if(start_time =='' || start_time == null){
				message = '开始时间不能为空！';
				is_check = false;
				return;
			}
			if(available_length =='' || available_length == null){
				message = '有效期不能为空！';
				is_check = false;
				return;
			}
		}else if(is_longterm_valid == '1'){
			$('#start_time_'+i).val('');
			$('#available_length_' + i).val('');
		}
	}); 
	
	if(!is_check){
		showInfoWin(message);
		return;
	}
	
	showSystemProccessing();
	var options = { 
		url: 'P_WF_ORDER!doReplyOperation.do',
		type: 'post',
		success: function(data){
			removeSystemProccessing();
			$.messager.alert("提示", "操作成功！", "info", function() {
				getOpenerWin().afterOK();
				window.close();
			});
		}
	};
	opeForm.ajaxSubmit(options);
}

//拒绝按钮操作
function refuseOperation() {
	var approve_content = $('#approve_content').val();
	if(isEmpty(approve_content)){
		showInfoWin('拒绝批复意见不能为空！');
		return ;
	}
	
	$ .messager.confirm('提示信息', '您确定要拒绝该申请记录吗?', function(result){
		if(result){
			var options = { 
					url: 'P_WF_ORDER!doRefuseOperation.do',
					type: 'post',
					success: function(data){
						$.messager.alert("提示", "操作成功！", "info", function() {
							getOpenerWin().afterOK();
							window.close();
						});
					}
				};
				opeForm.ajaxSubmit(options);
		}	
	});
}

//操作成功后刷新列表
function afterOK(){
	queryOperation();
}

//翻页函数
function turn(p){	    
	$('#page').val(p);
	opeForm.attr('action', 'P_WF_ACL_APPLY!showUsrNotApplyedRsc.do?firstQuery=false');
    opeForm.submit();
}
